package es.upv.tricia;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.annotations.SerializedName;

public class HighScoreList
{
	@SerializedName("scores")
	private List<HighScore> scores = new ArrayList<HighScore>();

	public List<HighScore> getScores()
	{
		return scores;
	}

	public void setScores(List<HighScore> scores)
	{
		this.scores = scores;
	}

	public List<HighScore> asSortedList()
	{
		Map<String, HighScore> highScores = new HashMap<String, HighScore>();

		for (HighScore score : scores) {
			String username = score.getUsername();

			if (!highScores.containsKey(username)
					|| highScores.get(username).getScore() < score.getScore()) {
				highScores.put(username, score);
			}
		}

		List<HighScore> result = new ArrayList<HighScore>(highScores.values());
		Collections.sort(result);

		return result;
	}
}
